package com.scalar.chiptrack.operations.process.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.HashMap;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.forms.BaseProductForm;
import com.scalar.ScalarException;


public final class BaseProductListAction extends ChipTrackAction {

    public BaseProductListAction() {
    }

private static final String ACTION_NAME = "/baseProductList.do";

public ActionForward execute (ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response) throws Exception
{
        HashMap<String, String> info    = new HashMap<String, String>();
        HashMap<String, String> errors  = new HashMap<String, String>();
        boolean success = false;
        BaseProductForm productForm = (BaseProductForm)form;
        HttpSession mSession        = request.getSession();
        DBTransaction dbTransaction = null;
        User user                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        String action = request.getParameter ( "action" );

        try
        {
            if ( action != null && action.equalsIgnoreCase( Constants.ACTION_ACTIVATE )  )
            {

                String baseProductIDStr = request.getParameter("baseProductID");
                int baseProductIdInt = 0;
                try
                {
                    baseProductIdInt = Integer.parseInt( baseProductIDStr );
                }
                catch(NumberFormatException nfe)
                {
                    baseProductIdInt = 0;
                }

                dbTransaction = DBTransaction.getInstance();
                dbTransaction.begin();

                ProductDao.updateProductFamilyStatus(dbTransaction, baseProductIdInt, 1, user.getUserName());
                //info.put( "Product activation successfully completed", null );
            }
            else if ( action != null && action.equalsIgnoreCase( Constants.ACTION_OBSOLETE ) )
            {
                String baseProductIDStr = request.getParameter("baseProductID");

                int baseProductIdInt = 0;
                try
                {
                    baseProductIdInt = Integer.parseInt( baseProductIDStr );
                }
                catch(NumberFormatException nfe)
                {
                    baseProductIdInt = 0;
                }

                dbTransaction = DBTransaction.getInstance();
                dbTransaction.begin();

                ProductDao.updateProductFamilyStatus(dbTransaction, baseProductIdInt, 0, user.getUserName() );
                //info.put( "Product " + action + " successfully completed", null );
            }
            success = true;
        }
        catch(ScalarException se)
        {
            success = false;
            errors.put( "Unable to " + action + " Product.", null );
        }
        finally
        {
            if( dbTransaction != null )
            {
                if( success == true )
                {
                    try
                    {
                        dbTransaction.commit();
                        info.put( "Product " + action + " successfully completed", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        errors.put ( "Error while committing the database ", null );
                    }
                }
                else
                {
                    try
                    {
                        dbTransaction.rollback();
                        errors.put ( "Product update status aborted", null );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                        errors.put ( "Error occurred while rolling back", null );
                    }
                }
            }
        }

        if ( action != null && action.equalsIgnoreCase("searchNew") )
        {
            request.setAttribute("productList",ProductDao.getBaseProductsByPartNumber( productForm.getPartNumber() ));

            if ( mSession.getAttribute( "partNumberList" ) == null )
            {
                mSession.setAttribute("partNumberList", ProductDao.getAllProductsByType() );
            }
            //request.setAttribute("partNumberList", partNumberList);

            return (mapping.findForward("success") );
        }

        mSession.setAttribute("partNumberList", ProductDao.getAllProductsByType() );
        request.setAttribute("productList",ProductDao.getBaseProducts());

        request.setAttribute(SessionKeys.INFO_MESSAGES, info);
        request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);

        return ( mapping.findForward("success") );
	}
}